package com.lanou.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lanou.entity.TbItemDesc;
import com.lanou.mapper.TbItemDescMapper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/11/19
 * <p>
 * 通过传递过来的条件参数进行数据库tb_item_desc表查询
 * 再将查询结果插入到Redis中
 */
public class ItemDescRedis implements Runnable {

    private TbItemDescMapper tbItemDescMapper;

    private JedisPool jedisPool;

    private Integer startPage;

    private Integer rows;

    public ItemDescRedis(TbItemDescMapper tbItemDescMapper, JedisPool jedisPool, Integer startPage, Integer rows) {
        this.tbItemDescMapper = tbItemDescMapper;
        this.jedisPool = jedisPool;
        this.startPage = startPage;
        this.rows = rows;
    }

    @Override
    public void run() {
        Map<String, Object> map = new HashMap<>();
        map.put("startPage", startPage);
        map.put("rows", rows);
        List<TbItemDesc> itemDescList = tbItemDescMapper.selectByStartPageRows(map);
        for (TbItemDesc tbItemDesc : itemDescList) {
            Jedis jedis = jedisPool.getResource();
            String jsonString = JSON.toJSONString(tbItemDesc, SerializerFeature.WriteClassName);
            jedis.set("desc" + tbItemDesc.getItemId(), jsonString);
            System.out.println("desc" + tbItemDesc.getItemId());
            jedis.close();
        }
    }
}
